package cn.kgc.chlm.dao.impl;

import cn.kgc.chlm.dao.OrderDao;
import cn.kgc.chlm.entity.Menu;
import cn.kgc.chlm.entity.Order;
import cn.kgc.chlm.entity.User;
import cn.kgc.chlm.utils.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {

    private Connection conn;
    private PreparedStatement pstmt;
    private ResultSet rs;

    @Override
    public List<Order> selectOrderByUserId(User user) {

        List<Order> orders = null;

        try {
            conn = JdbcUtil.getConnection();
            String orderSql = "SELECT o.id,u.name,m.name,u.mobile_phone,o.address,o.price,o.num,o.count_price FROM `order` AS o\n" +
                    "INNER JOIN `user` AS u\n" +
                    "ON o.user_id = u.id\n" +
                    "INNER JOIN `menu` AS m\n" +
                    "ON o.menu_id = m.id\n"+
                    " where u.id = ?  and o.state = 1";
            pstmt = conn.prepareStatement(orderSql);
            pstmt.setInt(1,user.getId());
          //  System.out.println(pstmt);
            rs = pstmt.executeQuery();
            orders = new ArrayList<>();
            Order order = null;
            Menu menu = null;
            while (rs.next()){
                //实例化对象
                order = new Order();
                menu = new Menu();
                menu.setName(rs.getString("m.name"));
                order.setId(rs.getInt("o.id"));
                order.setAddress(rs.getString("o.address"));
                order.setPrice(rs.getDouble("o.price"));
                order.setCountPrice(rs.getDouble("o.count_price"));
                //Order对象中传入User和Menu对象
                order.setUser(user);
                order.setMenu(menu);
                //把该Order存入集合
                orders.add(order);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //返回集合
        return orders;
    }

    @Override
    public Order selectOrderById(Integer id) {

        Order order = null;
        try {
            conn = JdbcUtil.getConnection();
            String orderSql = "SELECT o.id,u.name,m.name,u.mobile_phone,o.address,o.price,o.num,o.count_price FROM `order` AS o\n" +
                    "INNER JOIN `user` AS u\n" +
                    "ON o.user_id = u.id\n" +
                    "INNER JOIN `menu` AS m\n" +
                    "ON o.menu_id = m.id\n"+
                    " where o.id = ? and o.state = 1";
            pstmt = conn.prepareStatement(orderSql);
            pstmt.setInt(1,id);
            //  System.out.println(pstmt);
            rs = pstmt.executeQuery();

            Menu menu = null;
            while (rs.next()){
                //实例化对象
                order = new Order();
                menu = new Menu();
                menu.setName(rs.getString("m.name"));
                order.setId(rs.getInt("o.id"));
                order.setAddress(rs.getString("o.address"));
                order.setPrice(rs.getDouble("o.price"));
                order.setNum(rs.getInt("o.num"));
                order.setCountPrice(rs.getDouble("o.count_price"));
                //Order对象中传入User和Menu对象
                order.setMenu(menu);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //返回集合
        return order;

    }

    @Override
    public int addOrder(Order order) {
        int n = 0;
        try {
            conn = JdbcUtil.getConnection();
            System.out.println("order:"+conn);
            String addOrderSql = "insert into `order`(user_id,menu_id,num,address,price,count_price,state) values(?,?,?,?,?,?,1)";
            pstmt = conn.prepareStatement(addOrderSql);
            pstmt.setInt(1,order.getUserId());
            pstmt.setInt(2,order.getMenuId());
            pstmt.setInt(3,order.getNum());
            pstmt.setString(4,order.getAddress());
            pstmt.setDouble(5,order.getPrice());
            pstmt.setDouble(6,order.getCountPrice());
            n = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return n;
    }

    @Override
    public int delOrder(Integer id) {

        int n = 0;
        try {
            conn = JdbcUtil.getConnection();
            //正规项目不允许真实删除数据
//            String orderSql = "delete from `order` where id = ?";
            //正规项目中数据库的数据不真实删除，而是更改状态码来实现。
            String delSql = "update `order` set state = 0 where id = ?";
            pstmt = conn.prepareStatement(delSql);
            pstmt.setInt(1,id);
            //  System.out.println(pstmt);
            n = pstmt.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


        return n;
    }
}
